package com.origin.niuke.dynamic;

/**
 * NC108 最大正方形
 * 算法：动态规划
 *
 * @author yzh
 * @version 1.0
 * @date 2022/8/23 21:44
 */
public class NC108 {

    public int solve (char[][] matrix) {
        if (matrix.length == 0) return 0;
        int ans = 0, m = matrix.length, n = matrix[0].length;
        int[][] dp = new int[m + 1][n + 1];
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (matrix[i - 1][j - 1] == '1') {
                    dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1;
                    ans = Math.max(ans, dp[i][j]);
                }
            }
        }
        return ans * ans;
    }

}
